package org.ifilm.controller;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.ifilm.model.Project;
import org.ifilm.service.ProjectService;
import org.ifilm.util.GeneralConstants;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

/**
 * 
 * Project Contoller. All requests about projects will be here
 * @author Mustafa Genç
 *
 */
@Controller
@RequestMapping("/project/**")
public class ProjectController {
	@Autowired
	private ProjectService projectService;
	
	@RequestMapping(value = "/createProject", method = {RequestMethod.POST, RequestMethod.GET})
	public ResponseEntity<Map<String, Object>> createUser(HttpServletRequest request, HttpServletResponse response) {
		Map<String, Object> responseBody = new HashMap<String, Object>();
		
		Project project = new Project();
		project.setProjectName(request.getParameter(GeneralConstants.projectName));
		
		Project createdProject = projectService.createProject(project);
		
		responseBody.put("project", createdProject);
		
		return new ResponseEntity<Map<String, Object>>(responseBody, HttpStatus.OK);
	}
	
	@RequestMapping(value = "/getProject", method = {RequestMethod.POST, RequestMethod.GET})
	public ModelAndView getProject(@RequestParam(value = "pid") Long id, 
			HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		ModelAndView result = new ModelAndView();
		
		Project project = projectService.getProject(id);
		if (project != null) {
			result.setViewName("/projectDetail.jsp");
			result.addObject("project", project);
		}
		
		return result;
	}
	
	@RequestMapping(value = "/listProjects", method = {RequestMethod.POST, RequestMethod.GET})
	public ModelAndView listAllProjects(@RequestParam(value = "page") Integer page,
			@RequestParam(value = "max") Integer max, HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		ModelAndView result = new ModelAndView("/homeDraft.jsp");
		
		List<Project> projectList = projectService.listProjects(page, max);
		result.addObject("projects", projectList);
		result.addObject("page", page);
		result.addObject("max", max);
		
		return result;
	}
	
	@RequestMapping(value = "/getRecommendedPostings", method = {RequestMethod.POST, RequestMethod.GET})
	public ResponseEntity<Map<String, Object>> getRecommendedPostings(HttpServletRequest request, HttpServletResponse response) {
		Map<String, Object> responseBody = new HashMap<String, Object>();
		
		Project project = new Project();
		project.setProjectName(request.getParameter(GeneralConstants.projectName));
		
		Project createdProject = projectService.createProject(project);
		
		responseBody.put("project", createdProject);
		
		return new ResponseEntity<Map<String, Object>>(responseBody, HttpStatus.OK);
	}
}